﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DBUtility;

namespace SQLServerDAL.PageConfig
{
    public class BannerConfig
    {
        public BannerConfig() { }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="_obj"></param>
        /// <returns></returns>
        public string Add(Model.PageConfig.BannerConfig _obj)
        {
            string result = "";

            string sqlstring = "Declare @ConfigId Uniqueidentifier; SET @ConfigId=NEWID(); Insert Into [PageConfigBase] Values(@ConfigId,@RegardingObjectId,@RegardingObjectName,@RegardingObjectTypeCode,@RegardingObjectTypeName,@ShowPosition,@ShowPositionCode,@LinkURL,@Picture1,@StatusCode,@CreatedBy,@CreatedOn,@ModifyBy,@ModifyOn,@Text1);SELECT @ConfigId";

            SqlParameter[] prams = {
				SqlHelper.CreateInputParam("@RegardingObjectId", SqlDbType.UniqueIdentifier, new Guid(_obj.RegardingObjectId)),
				SqlHelper.CreateInputParam("@RegardingObjectName", SqlDbType.NVarChar, _obj.RegardingObjectName),
				SqlHelper.CreateInputParam("@RegardingObjectTypeCode", SqlDbType.Int, _obj.RegardingObjectTypeCode),
				SqlHelper.CreateInputParam("@RegardingObjectTypeName", SqlDbType.NVarChar, _obj.RegardingObjectTypeName),
				SqlHelper.CreateInputParam("@ShowPosition", SqlDbType.VarChar, _obj.ShowPosition),
				SqlHelper.CreateInputParam("@ShowPositionCode", SqlDbType.Int, _obj.ShowPositionCode),
                SqlHelper.CreateInputParam("@LinkURL", SqlDbType.VarChar, _obj.LinkURL),
				SqlHelper.CreateInputParam("@Picture1", SqlDbType.VarChar, _obj.Picture1),
				SqlHelper.CreateInputParam("@StatusCode", SqlDbType.Int, _obj.StatusCode),
                SqlHelper.CreateInputParam("@CreatedBy", SqlDbType.UniqueIdentifier, new Guid(_obj.CreatedBy)),
                SqlHelper.CreateInputParam("@CreatedOn", SqlDbType.DateTime, _obj.CreatedOn),
                SqlHelper.CreateInputParam("@ModifyBy", SqlDbType.UniqueIdentifier, new Guid(_obj.ModifyBy)),
                SqlHelper.CreateInputParam("@ModifyOn", SqlDbType.DateTime, _obj.ModifyOn),
				SqlHelper.CreateInputParam("@Text1", SqlDbType.NVarChar, _obj.Text1)
			};

            try
            {
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlstring, prams))
                {
                    rdr.Read();

                    result = "succeeded" + "|" + rdr[0].ToString();
                }
            }
            catch (Exception ex)
            {
                result = ex.ToString();
            }

            return result;
        }
        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="_obj"></param>
        /// <returns></returns>
        public string Update(Model.PageConfig.BannerConfig _obj)
        {
            string result = "";

            SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);
            conn.Open();
            using (SqlTransaction trans = conn.BeginTransaction())
            {
                try
                {
                    string sqlstring = "UPDATE [PageConfigBase] SET [RegardingObjectId] = @RegardingObjectId,[RegardingObjectName] = @RegardingObjectName,[RegardingObjectTypeCode] = @RegardingObjectTypeCode,[RegardingObjectTypeName] = @RegardingObjectTypeName,[ShowPosition] = @ShowPosition,[ShowPositionCode] = @ShowPositionCode,[LinkURL] = @LinkURL,[Picture1] = @Picture1,[StatusCode] = @StatusCode,[ModifyBy] = @ModifyBy,[ModifyOn] = @ModifyOn, Text1=@Text1 Where ConfigId = @ConfigId";
                    SqlParameter[] prams = {
						SqlHelper.CreateInputParam("@ConfigId", SqlDbType.UniqueIdentifier, new Guid(_obj.ConfigId)),
				        SqlHelper.CreateInputParam("@RegardingObjectId", SqlDbType.UniqueIdentifier, new Guid(_obj.RegardingObjectId)),
				        SqlHelper.CreateInputParam("@RegardingObjectName", SqlDbType.NVarChar, _obj.RegardingObjectName),
				        SqlHelper.CreateInputParam("@RegardingObjectTypeCode", SqlDbType.Int, _obj.RegardingObjectTypeCode),
				        SqlHelper.CreateInputParam("@RegardingObjectTypeName", SqlDbType.NVarChar, _obj.RegardingObjectTypeName),
				        SqlHelper.CreateInputParam("@ShowPosition", SqlDbType.VarChar, _obj.ShowPosition),
				        SqlHelper.CreateInputParam("@ShowPositionCode", SqlDbType.Int, _obj.ShowPositionCode),
                        SqlHelper.CreateInputParam("@LinkURL", SqlDbType.VarChar, _obj.LinkURL),
				        SqlHelper.CreateInputParam("@Picture1", SqlDbType.VarChar, _obj.Picture1),
				        SqlHelper.CreateInputParam("@StatusCode", SqlDbType.Int, _obj.StatusCode),
                        SqlHelper.CreateInputParam("@ModifyBy", SqlDbType.UniqueIdentifier, new Guid(_obj.ModifyBy)),
                        SqlHelper.CreateInputParam("@ModifyOn", SqlDbType.DateTime, _obj.ModifyOn),
				        SqlHelper.CreateInputParam("@Text1", SqlDbType.NVarChar, _obj.Text1)
					};
 
                    SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sqlstring, prams);

                    trans.Commit();
                    result = "succeeded";
                }
                catch (Exception ex)
                {
                    result = ex.Message;
                    trans.Rollback();
                    throw ex;
                }
            }
            conn.Close();

            return result;
        }
        /// <summary>
        /// 匹配ID初始化对象属性
        /// </summary>
        /// <param name="configid"></param>
        /// <returns></returns>
        public Model.PageConfig.BannerConfig GetObjectById(string configid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM PageConfigBase WHERE ConfigId = @ConfigId");

            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ConfigId", SqlDbType.UniqueIdentifier) };
            parameters[0].Value = new Guid(configid);

            Model.PageConfig.BannerConfig __obj = null;
            try
            {
                SqlDataReader rd = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql.ToString(), parameters);
                if (rd.Read())
                {
                    __obj = new Model.PageConfig.BannerConfig();
                    __obj.RegardingObjectId = rd["RegardingObjectId"].ToString();
                    __obj.RegardingObjectName = rd["RegardingObjectName"].ToString();
                    __obj.RegardingObjectTypeCode = int.Parse(rd["RegardingObjectTypeCode"].ToString());
                    __obj.RegardingObjectTypeName = rd["RegardingObjectTypeName"].ToString();
                    __obj.ShowPosition = rd["ShowPosition"].ToString();
                    __obj.ShowPositionCode = int.Parse(rd["ShowPositionCode"].ToString());
                    __obj.LinkURL = rd["LinkURL"].ToString();
                    __obj.Picture1 = rd["Picture1"].ToString();
                    __obj.Text1 = rd["Text1"].ToString();
                    __obj.StatusCode = int.Parse(rd["StatusCode"].ToString());
                    __obj.CreatedBy = rd["CreatedBy"].ToString();
                    __obj.CreatedOn = DateTime.Parse(rd["CreatedOn"].ToString());
                    __obj.ModifyBy = rd["ModifyBy"].ToString();
                    __obj.ModifyOn = DateTime.Parse(rd["ModifyOn"].ToString());
                }
            }
            catch
            {
                return null;
            }
            return __obj;
        } 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="configid"></param>
        public void Delete(string configid)
        {
            StringBuilder strSql = new StringBuilder();
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ConfigId", SqlDbType.UniqueIdentifier) };
            parameters[0].Value = new Guid(configid);
 
            strSql.Append("DELETE FROM PageConfigBase WHERE [ConfigId] = @ConfigId;");

            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql.ToString(), parameters);
        }
    }
}
